import {observable, action} from 'mobx';
import isEqual from 'lodash/isEqual';


import {REPORT_TYPE} from '../components/Component/ReportList/Const';
import * as Apis from '../apis/report'

import {message} from 'antd'

import {WeaLocaleProvider} from "ecCom";
import { WeaForm } from 'comsMobx';

const getLabel = WeaLocaleProvider.getLabel;

const changeTableRow = (store, func) => {
  let tableInfo = {...store.table.tableInfo} || {}, tableBody = tableInfo.tableBody || [];
  tableBody = tableBody.map(func);
  tableInfo.tableBody = tableBody;
  return tableInfo;
};


export default class Report {
  test = getLabel(26470, "微博报表");
  @observable loading = false;
  @observable userList = [];
  @observable userInfo = {};

  @observable list = {};
  @observable table = {};
  @observable owner = {
    key: REPORT_TYPE.my,
    title: getLabel(18040, "我的报表")
  };//当前所查看报表
  @observable hasMood = false;//是否拥有心情模块
  @observable currentParams = {};//当前搜索条件
  @observable data = {};

  //新建自定义报表名称
  @observable addReportForm = new WeaForm();
  @observable 
  moduleStatus = {
    showCustomCreate: false,
    createOrEdit: 'create',
    editKey: -1,
    customDialogFields: []
  }

  @action doInit = () => {
    this.owner = {
      key: REPORT_TYPE.my,
      title: getLabel(18040, "我的报表")
    };
  }


  @action startLoad = (loading) => {
    this.loading = loading;
  }

  @action doLoading = loading => {
    this.loading = loading;
  }

  @action getUserInfo = params => {
    Apis.getUserInfo(params)
      .then(action(
        result => {
          this.userInfo = result;
        }
      )).catch(
      error => {
        message.error(error)
      }
    )

  }

  @action getReportList = params => {
    Apis.getReportList(params)
      .then(action(
        result => {
          this.list = result;
        }
      )).catch(
      error => {
        message.error(error)
      }
    )

  }

  @action addReport = (params, callback) => {
    this.startLoad(true)
    Apis.addReport(params)
      .then(action(
        result => {
          this.startLoad(false)
          message.success(getLabel('83885', "操作成功！"))
          let finalResult = {...this.list}
          finalResult.reportList.push(result.newReport || {})
          this.list = finalResult;
          if (callback) callback(result)
        }
      )).catch(
      error => {
        message.error(error)
      }
    )

  }

  @action deleteReport = (params, callback) => {
    this.startLoad(true)
    Apis.deleteReport(params)
      .then(action(
        result => {
          this.startLoad(false)
          message.success(getLabel('83885', "操作成功！"))
          let finalResult = {reportList: []}
          this.list.reportList.forEach(
            item => {
              if (item.key != params.id) {
                finalResult.reportList.push(item)
              }
            }
          )
          this.list = finalResult;
          this.getBlogReportConfig();
          this.getReportList();
          this.doInit();
          if (callback) callback(result)
        }
      )).catch(
      error => {
        message.error(error)
      }
    )

  }

  @action startLoadList = (loading, currentParams) => {
    this.loading = loading;
    this.currentParams = currentParams;
  }

  @action getReportTable = params => {
    let currentParams = {...params, _func: 'getReportTable'}
    this.startLoadList(true, currentParams)
    Apis.getReportTable(params)
      .then(action(
        result => {
          if (!isEqual(this.currentParams, currentParams))
            return;
          this.table = result;
          this.loading = false;
        }
      )).catch(
      error => {
        message.error(error)
      }
    )
  }

  @action getAttentionReport = (params) => {
    let currentParams = {...params, _func: 'getAttentionReport'}
    this.startLoadList(true, currentParams)
    Apis.getAttentionReport(params).then(action((result) => {


      if (!isEqual(this.currentParams, currentParams)) {
        return;
      }

      this.table = result;
      this.loading = false;
    }))

  }

  @action chooseOwner = owner => {
    let reportList = this.list.reportList || [];
    let ownerKey = owner;
    let owners = reportList.filter((report) => report.key == ownerKey);
    this.owner = owners.length > 0 ? owners[0] : {};
  }

  @action getTempReportTableInfo = (params) => {
    let currentParams = {...params, _func: 'getTempReportTableInfo'}
    this.startLoadList(true, currentParams)
    Apis.getTempReportTableInfo(params).then(action((result) => {
      if (!isEqual(this.currentParams, currentParams)) return;
      this.table = result;
      this.data=result.tableInfo;
      this.loading = false;
    }))
  }

  @action deleteTempCondition = (params, callback) => {
    Apis.deleteTempCondition(params).then(action((result) => {
      callback && callback();
    })).catch((e) => {
      message.error(e);
    })

  };

  @action getBlogReportConfig = (params) => {
    Apis.getBlogReportConfig(params).then(action(result => {
      this.hasMood = !!result.isMood;
    }));

  };

  @action getTempReportExpandRows = params => {
    let table = null;
    table = {...this.table};
    table.tableInfo = changeTableRow(this, (row) => {
      if (params.conditionId == row[0].conditionId) {
        let newRow = [...row];
        newRow[0].loading = !newRow[0].loading;
        return newRow;
      }
      return row;
    })
    this.table = table;
    Apis.getTempReportExpandRows(params).then(action(result => {
      table.tableInfo = changeTableRow(this, (row) => {
        if (params.conditionId == row[0].conditionId) {
          let newRow = [...row];
          newRow[0].subList = result;
          newRow[0].open = true;
          newRow[0].loading = false;
          return newRow;
        }
        return row;
      })
      this.table = table;
    }))
  };

  //???? 这个后续要不要改
  @action toggleConditionExpand = conditionId => {
    let table = {...this.table};
    table.tableInfo = changeTableRow(this, (row) => {
      if (conditionId == row[0].conditionId) {
        let newRow = [...row];
        newRow[0].open = !newRow[0].open;
        return newRow;
      }
      return row;
    })
    this.table = {...table}
  };


  @action editCustomizeReport = (params, callback) => {
    this.startLoad(true)
    Apis.editCustomizeReport(params)
      .then(action(
        result => {
          this.startLoad(false)
          if (!result.newReport) return;

          message.success(getLabel('83885', "操作成功！"))
          let finalResult = {...this.list};
          finalResult.reportList = finalResult.reportList.map((r) => {
            if (r.key == result.newReport.key)
              return result.newReport;
            else
              return r;
          });
          this.list = finalResult;
          this.owner = result.newReport;
          if (callback) callback(result)
        }
      )).catch(
      error => {
        message.error(error)
      }
    )

  };


  @action
  addUser(params) {
    this.userList.push({name: "dyf"});
  }

  @action
  getUserList(params) {
    return this.userList.slice();
  }

  @action getBlogCustomReportAddCondition = params => {
    Apis.getBlogCustomReportAdd(params).then(result => {
      this.addReportForm.initFormFields(result.conditioninfo);
      this.moduleStatus.customDialogFields = result.conditioninfo;
    }).catch(error => {
      message.warning(error);
    });
  }

  setModuleState = (params = {}) => {
    let moduleStatus = {...this.moduleStatus};
    Object.keys(params).forEach(key => {
      moduleStatus[key] = params[key];
    });
    this.moduleStatus = moduleStatus;
  }
}